package Models::MenuModel;
# mapper class
use lib 'd:/test_project/lib/';
use Models::db::Menu;
#use base qw(Models::Sql Backbone::TB);
#use base Models::BaseModel;
use strict;
use Data::Dumper;   
use DBI;

my $tablename = "mmenu";
my $dbh;

	BEGIN {	
		$dbh = DBI->connect('dbi:ODBC:driver={SQL Server};Server=localhost\SQLEXPRESS;database=testproject;uid=bohdan;pwd=test;',undef,undef,{ RaiseError => 0, AutoCommit => 0 })
				or print "Content-type: text/html\r\n\r\n connect failed: $DBI::errstr";
	} 
	END {
		$dbh->disconnect();
	}
  
	sub new {
		my $check = shift;
		my $class = ref($check) || $check;
		my $self  = {
			statusNumber => undef,
			statusMsg    => undef,
			statusLogID  => undef,
			@_ # init
		};
		$self->{Rows} = undef;
		$self->{Columns} = undef;
		return bless($self, $class);
	} 

	sub findPk {
		my ($self, $id) = @_;
		my $sth = $dbh->prepare("SELECT DISTINCT * FROM $tablename WHERE id=$id");
		$sth->execute();
		my $result = $sth->fetchrow_hashref();
		return Models::db::Menu->new($result);
	}

	sub getFullMenu {
		my $self = shift;		 
		my $query = $dbh->prepare("SELECT $tablename.*,content.code as code FROM $tablename LEFT OUTER JOIN content ON ($tablename.fk_content_id = content.id) WHERE $tablename.active=1 ORDER BY position ASC"); 
		$query->execute();
		my @res;
		#print "Content-type: text/html\n\n";
		while (my $row = $query->fetchrow_hashref()) {
			#print Dumper $row;
			push(@res,Models::db::Menu->new($row));
		}
		return @res;
	}
	
	sub getTableName {
		return $tablename;
	}


1;